<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use think\Db;

/**
 * 医生 tian 126
 */
class Doctor extends Api
{
    protected $noNeedLogin = ['doctor_lists', 'department_list', 'doctor_lists', 'doctor_detail', 'area_list', 'getRegionList', 'index_search','article_comment_click','index_banner'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     *
     */
    public function index()
    {
        $this->success('', ['welcome' => $this->auth->nickname]);
    }

    /**
     * 科室列表
     *
     * @ApiMethod (POST)
     * @param string $pid
     */
    public function department_list()
    {
        $Articles= new \app\common\model\Articles;
        $Category= new \app\common\model\Category;
        $pid = $this->request->post('pid',21);
        $data_one=$Category->where("pid=".$pid." and deletetime is null")->select();
        foreach($data_one as $key=>$value){
            $data_one[$key]["sons"]=$Category->where("pid=".$value["id"])->select();
        }

        $this->success("获取成功", $data_one);
    }

    /**
     * 省市区列表
     *
     * @ApiMethod (POST)
     * @param string $pid
     */
    public function area_list()
    {
        $ShoproArea= new \app\common\model\ShoproArea;
        $pid = $this->request->post('pid',0);
        $data_one=$ShoproArea->where("pid=".$pid)->select();
        foreach($data_one as $key=>$value){
            $data_one[$key]["sons_two"]=$ShoproArea->where("pid=".$value["id"])->select();
            if(!empty($data_one[$key]["sons_two"])){
                foreach($data_one[$key]["sons_two"] as $key2=>$value2){
                    //var_dump($value2["id"]);
                    $data_one[$key]["sons_two"][$key2]["sons_thr"]=$ShoproArea->where("pid=".$value2["id"])->select();
                    //$yyy=$ShoproArea->where("pid=".$value2["id"])->select();
                    //var_dump($yyy);die;
                }
            }
        }
        $this->success("获取成功", $data_one);
    }

    /**
     * 获取全国省市区列表（格式化后）
     */
    public function getRegionList()
    {
        $ShoproArea= new \app\common\model\ShoproArea;
        $data = $ShoproArea->select()->toArray();
        var_dump($data);die;
        $data = $this->assembleRegionData($data);
        return $data;
    }


    /**
     * 重组地区数据
     */
    public function assembleRegionData($data, $pid = 0)
    {
        $list = [];
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                // 继续查找其子市区县
                $v['list'] = $this->assembleRegionData($data, $v['id']);
                // 如果子地区为空，则unset掉
                if (empty($v['list'])) {
                    unset($v['list']);
                }
                $list[] = $v;
            }
        }
        return $list;
    }

    /**
     * 首页banner图
     *
     * @ApiMethod (POST)
     * @param string $department_id  科室类别id
     */
    public function index_banner()
    {
        $Doctor= new \app\common\model\Doctor;
        $ShoproDecorateContent= new \app\common\model\ShoproDecorateContent;
        $decorate_id = $this->request->post('decorate_id',1);
        if(empty($decorate_id)){
            $this->error("模板id必传");
        }
        $data=$ShoproDecorateContent
//            ->alias("a")
//            ->join("fa_category c", "c.id = a.category_id", "LEFT")
//            ->where("type="."'banner' and decorate_id=".$decorate_id." and id=13")
            ->where("type="."'banner' and decorate_id=".$decorate_id)
//            ->field("a.*, c.name type_name")
            ->order("id asc")
            ->select();
        if(!empty($data)){
            $data=$data[0];
        }
        $this->success("获取成功", $data);
    }


    /**
     * 医生列表
     *
     * @ApiMethod (POST)
     * @param string $department_id  科室类别id
     */
    public function doctor_lists()
    {
        //var_dump(88888);die;
        $Doctor= new \app\common\model\Doctor;
        $Category= new \app\common\model\Category;
        $department_id = $this->request->post('department_id');
        $keyword= $this->request->post('keyword');
        $province = $this->request->post('province');
        $city = $this->request->post('city');
        $area = $this->request->post('area');


        $page =$this->request->request("page", 0);
        $limit = $this->request->request("limit", 10);
        if(empty($limit)) $limit = 10;
        if($limit > 20) $limit = 20;
        $offset = $page * $limit;


        $where["a.delete_time"]=null;
        $where_string= "a.delete_time is null";
        if(!empty($department_id)){
           $where["a.department_id"]=$department_id;
        }
        if(!empty($keyword)){
            $where_string=$where_string." and ( doctor_name like '%".$keyword."%' or doctor_position like '%".$keyword."%' or hospital_name like '%".$keyword."%'  or doctor_tags like '%".$keyword."%' )";

//            $where_string=$where_string."doctor_name like ";
//            $where['doctor_name']  = array('like',"%$keyword%");
        }
        if(!empty($province)){
            $province = mb_ereg_replace("市", "", $province);
            $province = mb_ereg_replace("县", "", $province);
            $where["a.province"]=$province;
        }
        if(!empty($city)){
            $where["a.city"]=$city;
        }
        if(!empty($area)){
            $where["a.area"]=$area;
        }

        $all_num=$Doctor
            ->alias("a")
            ->join("fa_category c", "c.id = a.department_id", "LEFT")
            ->where($where)
            ->where($where_string)
            ->field("a.*, c.name type_name")
            ->count();

        $data=$Doctor
            ->alias("a")
            ->join("fa_category c", "c.id = a.department_id", "LEFT")
            ->where($where)
            ->where($where_string)
            ->field("a.*, c.name type_name")
            ->order("id desc")
            ->limit("$offset, $limit")
            ->select();
        foreach($data as $key=>$value){
            if(empty($value["type_name"])){
                $data[$key]["type_name"]="";
            }
            if(strpos($value['goodat'],'src="/uploads') !== false){
                $data[$key]["goodat"]=str_replace('src="/uploads','src="'.request()->domain().'/uploads',$value['goodat']);
            }
        }
        //$this->success("获取成功", $data);
        $this->success55("获取成功",$data,1,null,array(),$all_num);
        $this->success("获取成功", $data);
    }


    /**
     * 首页搜索列表
     *
     * @ApiMethod (POST)
     * @param string $department_id  科室类别id
     */
    public function index_search()
    {
        $Doctor= new \app\common\model\Doctor;
        $Category= new \app\common\model\Category;
        $Articles= new \app\common\model\Articles;
        $ShoproGoods= new \app\common\model\ShoproGoods;
        $keyword= $this->request->post('keyword');
        $area = $this->request->post('area');
        $where["deletetime"]=null;
        $where_doc["a.delete_time"]=null;
        $where_art["delete_time"]=null;
        $where_string_goods= "deletetime is null";
        $where_string_doc= "a.delete_time is null";
        $where_string_art= "delete_time is null";
        if(!empty($keyword)){
            $where_string_doc=$where_string_doc." and ( doctor_name like '%".$keyword."%' or doctor_position like '%".$keyword."%' or hospital_name like '%".$keyword."%'  or doctor_tags like '%".$keyword."%' )";
        }
        if(!empty($keyword)){
            $where_string_goods=$where_string_goods." and ( title like '%".$keyword."%' or subtitle like '%".$keyword."%')";
        }

        if(!empty($keyword)){
            $where_string_art=$where_string_art." and ( title like '%".$keyword."%')";
        }
        $data["goods"]=$ShoproGoods
//            ->alias("a")
//            ->join("fa_shopro_goods_sku k", "k.goods_id = a.id", "LEFT")
            ->where($where)
            ->where($where_string_goods)
            //->field("a.*, k.name ku_name")
            ->order("id desc")
            ->limit(2)
            ->select();
//        foreach($data["goods"] as $key=>$value){
//            $data["goods"][$key]["sku"]=Db::table("fa_shopro_goods_sku")
//                ->where("goods_id=".$value["id"])
//                ->order("id desc")
//                ->select();
//        }
        $data["doctor"]=$Doctor
            ->alias("a")
            ->join("fa_category c", "c.id = a.department_id", "LEFT")
            ->where($where_doc)
            ->where($where_string_doc)
            ->field("a.*, c.name type_name")
            ->order("id desc")
            ->limit(2)
            ->select();
        foreach($data["doctor"] as $key=>$value){
            if(empty($value["type"])){
                $data["doctor"][$key]["type_name"]="";
            }
        }
        $data["article"]=$Articles
//            ->alias("a")
//            ->join("fa_category c", "c.id = a.category_id", "LEFT")
            ->where($where_art)
            ->where($where_string_art)
//            ->field("a.*, c.name type_name")
            ->order("id desc")
            ->limit(2)
            ->select();

        $this->success("获取成功", $data);
    }


    /**
     * 医生详情
     *
     * @ApiMethod (POST)
     * @param string $id  医生id
     */
    public function doctor_detail()
    {
        $Articles= new \app\common\model\Articles;
        $Category= new \app\common\model\Category;
        $Doctor= new \app\common\model\Doctor;
        $id = $this->request->post('id');
        if(empty($id)){
            $this->error("医生id必传");
        }
        $data=$Doctor->where("a.id=".$id." and delete_time is null")
            ->alias("a")
            ->join("fa_category c", "c.id = a.department_id", "LEFT")
            ->field("a.*, c.name type_name")
            ->order("id desc")
            ->find();
        if(empty($data["type_name"])){
            $data["type_name"]="";
        }
        if(strpos($data['goodat'],'src="/uploads') !== false){
            $data["goodat"]=str_replace('src="/uploads','src="'.request()->domain().'/uploads',$data['goodat']);
        }
        $this->success("获取成功", $data);
    }



}
